// Created by WXX on 2021/12/30 21:49
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

/**
 * 执行用时：72 ms, 在所有 C++ 提交中击败了55.76%的用户
 * 内存消耗：28.7 MB, 在所有 C++ 提交中击败了7.20%的用户
 */
class Solution {
public:
    bool isNStraightHand(vector<int> &hand, int groupSize) {

        int n = hand.size();
        if (n % groupSize) return false;

        multiset<int> S;
        for (auto x : hand) S.insert(x);
        while (S.size()) {
            int x = *S.begin();
            for (int i = x; i < x + groupSize; i++)
                if (!S.count(i)) return false;
                else S.erase(S.find(i));
        }
        return true;
    }
};

int main() {

    vector<int> hand = {1, 2, 3, 6, 2, 3, 4, 7, 8};
    cout << Solution().isNStraightHand(hand, 3) << endl;  // true

    return 0;
}
